package com.yixin.hospital.service.extend.impl;

import com.yixin.hospital.common.WebSocket;
import com.yixin.hospital.entity.domain.AuditFlow;
import com.yixin.hospital.entity.domain.AuditFlowDetail;
import com.yixin.hospital.entity.domain.Message;
import com.yixin.hospital.entity.dto.req.auditmanager.AddAuditFlowReqDTO;
import com.yixin.hospital.enums.AuditDetailStatusEnum;
import com.yixin.hospital.service.*;
import com.yixin.hospital.service.extend.AuditFlowPresetExtendService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AuditFlowPresetExtendServiceImpl implements AuditFlowPresetExtendService {

    @Resource
    private AuditFlowService auditFlowService;
    @Resource
    private AuditFlowDetailService auditFlowDetailService;
    @Resource
    private UserService userService;
    @Resource
    MessageService messageService;
    @Resource
    JobDutyService jobDutyService;
    @Resource
    DeptService deptService;

    @Override
    @Transactional
    public AuditFlow addAuditFlow(AddAuditFlowReqDTO addAuditFlowReqDTO) {
        AuditFlow auditFlow = new AuditFlow();
        auditFlow.setFlowNo(addAuditFlowReqDTO.getFlowNo());
        auditFlow.setTitle(addAuditFlowReqDTO.getTitle());
        auditFlow.setBusinessCode(addAuditFlowReqDTO.getBusinessCode());
        auditFlow.setUserId(addAuditFlowReqDTO.getUserId());
        auditFlow.setCreatedBy(addAuditFlowReqDTO.getUserName());
        auditFlow.setUpdatedBy(addAuditFlowReqDTO.getUserName());
        auditFlowService.save(auditFlow);

        // 添加审批流详情表
        List<AuditFlowDetail> auditFlowDetailList = new ArrayList<>();
        List<Long> auditUserIds = addAuditFlowReqDTO.getAuditUserIds();
        for (int i = 0; i < auditUserIds.size(); i++) {
            AuditFlowDetail auditFlowDetail = new AuditFlowDetail();
            auditFlowDetail.setFlowNo(addAuditFlowReqDTO.getFlowNo());
            auditFlowDetail.setBusinessCode(addAuditFlowReqDTO.getBusinessCode());
            auditFlowDetail.setAuditUserId(auditUserIds.get(i));
            auditFlowDetail.setSort(i + 1);
            auditFlowDetail.setCreatedBy(addAuditFlowReqDTO.getUserName());
            auditFlowDetail.setUpdatedBy(addAuditFlowReqDTO.getUserName());
            if (i == 0) {
                auditFlowDetail.setStatus(AuditDetailStatusEnum.AUDIT_ME.getCode());
                // 发送通知
                Message message = new Message();
                message.setMessageType(addAuditFlowReqDTO.getMessageType());
                message.setUserId(auditUserIds.get(i));
                message.setContent(addAuditFlowReqDTO.getTitle());
                message.setStatus(0);
                message.setCreatedBy(addAuditFlowReqDTO.getUserName());
                message.setUpdatedBy(addAuditFlowReqDTO.getUserName());
                messageService.save(message);
                WebSocket.sendMessage(addAuditFlowReqDTO.getTitle(), auditUserIds.get(i).toString());
            }
            auditFlowDetailList.add(auditFlowDetail);
        }

        auditFlowDetailService.saveBatch(auditFlowDetailList);
        return auditFlow;
    }
}
